{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9e33e871",
   "metadata": {},
   "source": [
    "# 测试我们的算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "71011de9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn import datasets "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b3018336",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename'])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = datasets.load_iris()\n",
    "iris.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0dfb32f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = iris.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5fcf60bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = iris.target"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ae1986c",
   "metadata": {},
   "source": [
    "## train_test_split"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39e82281",
   "metadata": {},
   "source": [
    "分离出一部分数据做训练，另外一部分数据做测试。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84065523",
   "metadata": {},
   "source": [
    "### 对索引进行乱序实现测试和训练随机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "83bd9d5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([120,   9, 125,  83,  82, 132,  40,  16, 105,  47,  34,  52,  84,\n",
       "       149,  50,  71, 117, 141,  33,  31,  29, 126, 102, 107,  65, 115,\n",
       "        57, 139,  79,  23,  54, 128, 118, 145, 129, 147,  74,  80, 119,\n",
       "         1, 148,  17, 116,  10,  49,  62, 146,  56, 135,  11, 144,  61,\n",
       "       122,  99, 136,  78, 142,  41, 130,   7,  66,  25, 111, 114,  21,\n",
       "       124,  30,  73,  12, 106,  44,  28,  32,  81,  51,  58,  88,  68,\n",
       "        86, 138,  72, 123, 113, 104, 140,   4,  85,  94,  35,  27,  20,\n",
       "       100,   0,  92,   6,  18,   5,  96, 131,  97,  69, 137,  90,  45,\n",
       "        22, 110,  38,  91,   3,  89,  24,  63,  95,  13,  36, 121,  15,\n",
       "       109,  26,  98,  53,  46,  19,  39,  14,  37, 134,  60, 127, 103,\n",
       "        43, 101,  59,   2,   8,  67, 133,  42,  55,  93,  77, 143,  64,\n",
       "        48,  76,  87, 112,  70,  75, 108])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shuffled_indexes = np.random.permutation(len(X))\n",
    "shuffled_indexes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d458345",
   "metadata": {},
   "source": [
    "取20%的数据作为测试数据集，80%的数据作为训练数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "54f335a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_ratio = 0.2\n",
    "test_size = int(len(X) * test_ratio)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f36c296b",
   "metadata": {},
   "source": [
    "取前20%的数据作为测试数据集，后80%的数据作为训练数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aba3cc90",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_indexes = shuffled_indexes[:test_size]\n",
    "train_indexes = shuffled_indexes[test_size:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d5b4f7ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = X[train_indexes]\n",
    "y_train = y[train_indexes]\n",
    "\n",
    "X_test = X[test_indexes]\n",
    "y_test = y[test_indexes]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f192f27b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(120, 4)\n",
      "(120,)\n"
     ]
    }
   ],
   "source": [
    "print(X_train.shape)\n",
    "print(y_train.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8d696eec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(30, 4)\n",
      "(30,)\n"
     ]
    }
   ],
   "source": [
    "print(X_test.shape)\n",
    "print(y_test.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38b3fb95",
   "metadata": {},
   "source": [
    "### 封装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4451978a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from playML.model_selection import train_test_split\n",
    "X_train, y_train, X_test, y_test = train_test_split(X, y, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "aaf9f836",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(120, 4)\n",
      "(120,)\n"
     ]
    }
   ],
   "source": [
    "print(X_train.shape)\n",
    "print(y_train.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "dd0a65d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(30, 4)\n",
      "(30,)\n"
     ]
    }
   ],
   "source": [
    "print(X_test.shape)\n",
    "print(y_test.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cd1e6d3",
   "metadata": {},
   "source": [
    "### 测试我们的算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "92d41bcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from playML.kNN import KNNClassifier\n",
    "my_knn_clf = KNNClassifier(3)\n",
    "my_knn_clf.fit(X_train, y_train)\n",
    "y_predict = my_knn_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a507acec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 2, 1, 1, 0, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 1, 0, 2, 0, 0, 2,\n",
       "       0, 1, 2, 1, 0, 2, 1, 0])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5846be16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 2, 1, 1, 0, 0, 2, 2, 0, 0, 2, 0, 2, 2, 0, 1, 0, 2, 0, 0, 2,\n",
       "       0, 1, 2, 1, 0, 2, 1, 0])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3b697773",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(y_predict == y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "95f6b126",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(y_predict == y_test) / len(y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "111983cc",
   "metadata": {},
   "source": [
    "### sklearn中的train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ce2bf52e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b1045b5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "19553c5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "118a0c88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(n_neighbors=3)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kNN_classifier = KNeighborsClassifier(n_neighbors=3)\n",
    "kNN_classifier.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "fe81af9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 1, 2, 0, 1, 1, 2, 1, 1, 1, 0, 0, 0, 2, 1, 0, 2, 2, 2, 1, 0,\n",
       "       2, 0, 1, 1, 0, 1, 2, 2])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_predict = kNN_classifier.predict(X_test)\n",
    "y_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f1ce2167",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(y_predict == y_test) / len(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf887d66",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
